package org.mbds.sensorsdatacollector.android.view;

import java.util.ArrayList;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class MyItemizedOverlay extends ItemizedOverlay {

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    private Context context;

    public MyItemizedOverlay(Drawable defaultMarker, Context ctx) {
		  super(boundCenterBottom(defaultMarker));
		  context = ctx;
	}
    
    @Override
    protected boolean onTap(int index) {
		if (index==0) {
			return true;
		}
		OverlayItem item = mOverlays.get(index);
		AlertDialog.Builder dialog = new AlertDialog.Builder(context);
		dialog.setTitle(item.getTitle());
		dialog.setMessage(item.getSnippet());
		dialog.show();
		return true;
    }

    
    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}
	
//	@Override
//	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
//		shadow = false;
//		for(OverlayItem currentPoint:mOverlays){
//	        GeoPoint geopoint =currentPoint.getPoint();
//	        Point point = new Point();
//	        Projection projection = mapView.getProjection();
//	        projection.toPixels(geopoint, point);                       
//	        BitmapDrawable bitDraw= ((BitmapDrawable)currentPoint.getMarker(0));
//	        Drawable currentMarker = currentPoint.getMarker(0); 
//	        /*if (currentMarker!=null){
//
//	            if (currentWidthRatio<.6f) {                
//	                currentWidthRatio = .6f;                    
//	            }
//	            else if (currentWidthRatio>1.5f) {
//	                currentWidthRatio = 1.5f;
//	            }
//	        }*/
//
//	        if (bitDraw!=null){         
//	        Bitmap bitmap = super.getResizedBitmap(bitDraw.getBitmap(),(int)(currentMarker.getIntrinsicHeight() * currentWidthRatio),
//	                (int)(currentMarker.getIntrinsicWidth()*currentWidthRatio));
//
//	        canvas.drawBitmap(bitmap,
//	                point.x - bitmap.getHeight()/2,
//	                point.y - bitmap.getWidth()/2, null);
//	        }else{
////	            Log.println(Log.DEBUG,"non-existent point", "");
//	        }
//		super.draw(canvas, mapView, shadow);
//		}
//	}
}
